import re

with open('input.txt') as file:
    memory = {}
    or_mask = 0
    and_mask = 0

    for line in file.read().splitlines():
        if line.startswith('mask'):
            mask = re.fullmatch(r'mask = ([X10]{36})', line).groups()[0]

            or_mask = int(mask.replace('X', '0'), 2)
            and_mask = int(mask.replace('X', '1'), 2)

        else:
            address, value = map(int, re.fullmatch(r'mem\[(\d+)\] = (\d+)', line).groups())

            memory[address] = (value | or_mask) & and_mask

    print(sum(memory.values()))